# -*- coding: utf-8 -*-




from django import forms
from utils.captcha.mycaptcha import Captcha
import json

class LoginForm(forms.Form):
	username = forms.CharField(max_length=10,min_length=4)
	password = forms.CharField(max_length=20,min_length=6)
	captcha = forms.CharField(max_length=4,min_length=4)
	remember = forms.BooleanField(required=False) #用户有可能不需要记住我,那么这个参数有可能就没有

	def clean_captcha(self):
		captcha = self.cleaned_data.get('captcha',None)
		if not Captcha.check_captcha(captcha):
			raise forms.ValidationError(u'验证码错误!')
		return captcha

	def get_error(self):
		errors = self.errors.as_json()
		error_dict = json.loads(errors)
		message = ''
		# 只取字典中的第一个值
		# {u'captcha': [{u'message': u'xxx', u'code': u''}]}
		for k,v in error_dict.items():
			message = v[0].get('message',None)
		return message

class UpdateProfileForm(forms.Form):
	avatar = forms.URLField(max_length=100,required=False)
	username = forms.CharField(max_length=10,min_length=4,required=False)

class UpdateEmailForm(forms.Form):
	email = forms.EmailField(required=True)

class AddCategoryForm(forms.Form):
	categoryname = forms.CharField(max_length=20)

class AddTagForm(forms.Form):
	tagname = forms.CharField(max_length=20)

class AddArticleForm(forms.Form):
	title = forms.CharField(max_length=200)
	category = forms.IntegerField(required=True)
	desc = forms.CharField(max_length=200,required=False)
	thumbnail = forms.URLField(max_length=100,required=False)
	content_html = forms.CharField()

	# author,tags
	# tags是通过数组的形式进行上传的,forms没有合适的
	# field来进行验证,所以不放在form中进行验证

	# author也不需要,因为当前登录的页面就是author

